<!-- ModalContainer.vue -->
<template>
  <teleport to="body">
    <transition :name="animation" @after-leave="handleAfterLeave">
      <div
        v-if="show"
        class="modal-overlay"
        :style="overlayStyle"
        @click.self="handleOverlayClick"
      >
        <div class="modal-content" :style="contentStyle">
          <div class="modal-header">
            <div>{{ header }}</div>
            <div class="close-btn" @click="onClose">✖</div>
          </div>
          <slot />
          <div v-if="!customButton" class="modal-footer">
            <button class="btn cancel-btn" @click="onClose">取消</button>
            <button class="btn confirm-btn" @click="onConfirm">确定</button>
          </div>
        </div>
      </div>
    </transition>
  </teleport>
</template>

<script setup>
import { ref, defineEmits, defineProps } from "vue";

const props = defineProps({
  show: Boolean,
  customButton: {
    type: Boolean,
    default: false,
  },
  /**弹窗标题 */
  header: {
    type: String,
    default: "系统提示",
  },
  /**点击遮罩层是否需要关闭 */
  closeOnClickOverlay: {
    type: Boolean,
    default: true,
  },
  /**弹窗动画 */
  animation: {
    type: String,
    default: "fade",
  },
  /**遮罩层样式 */
  overlayStyle: {
    type: Object,
    default: () => ({ backgroundColor: "rgba(0,0,0,0.2)" }),
  },
  contentStyle: Object,
});

const emit = defineEmits(["close", "after-close", "confirm"]);

const handleOverlayClick = () => {
  if (props.closeOnClickOverlay) {
    emit("close");
  }
};

const onClose = () => {
  emit("close");
};

const handleAfterLeave = () => {
  emit("after-close");
};

const onConfirm = () => {
  emit("confirm");
};
</script>

<style scoped lang="less">
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}

.modal-content {
  background: white;
  padding: 10px 20px;
  border-radius: 4px;
}

.modal-content .modal-header {
  margin-bottom: 12px;
  display: flex;
  justify-content: space-between;
  .close-btn {
    font-weight: bold;
    font-size: 16px;
    color: #555;
    cursor: pointer;
  }
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

.scale-enter-active,
.scale-leave-active {
  transition: all 0.3s ease;
}

.scale-enter-from,
.scale-leave-to {
  opacity: 0;
  transform: scale(0.8);
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  margin-top: 12px;
  .btn {
    padding: 8px 16px;
    border-radius: 4px;
    border: none;
    cursor: pointer;
    margin-left: 8px;
    transition: background-color 0.3s, color 0.3s;
  }
  .cancel-btn {
    background-color: #f5f5f5;
    color: #333;
    &:hover {
      background-color: #e0e0e0;
    }
  }
  .confirm-btn {
    background-color: #1890ff;
    color: white;
    &:hover {
      background-color: #40a9ff;
    }
  }
}
</style>
